package org.balabala.user.service.impl;



import com.alibaba.fastjson.TypeReference;
import org.balabala.user.dao.CommentDao;
import org.balabala.user.entity.BrowseInfoEntity;
import org.balabala.user.entity.CommentEntity;
import org.balabala.user.entity.VideoEntity;
import org.balabala.user.feign.VideoFeign;
import org.balabala.user.service.CommentService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Query;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {

    @Autowired
    private VideoFeign videoFeign;

    @Resource
    private CommentDao commentDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<CommentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils listByUid(Map<String, Object> hashMap) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(hashMap),
                new QueryWrapper<CommentEntity>().eq("uid",hashMap.get("uid"))
        );
        List<CommentEntity> records = page.getRecords();
        List<Long> collect = records.stream().map(item -> item.getVideoId()).collect(Collectors.toList());
        Long[] videoIds = collect.toArray(new Long[]{});
        //调用用户服务，进行视频信息的查询
        Resp resp = videoFeign.listByUid(videoIds);
        List<VideoEntity> data = resp.getData(new TypeReference<List<VideoEntity>>() {
        });
        //
        if(resp.getCode() == 0){
            for(int i=0;i<records.size();i++) {
                for(int j=0;j<data.size();j++){
                    if(records.get(i).getVideoId() == data.get(j).getVideoId()){
                        VideoEntity videoEntity = data.get(j);
                        records.get(i).setVideoEntity(videoEntity);
                        break;
                    }
                }
            }
        }else{
            return null;
        }
        return new PageUtils(page);
    }

    @Override
    public Resp removeByCommentId(Long commentId) {
        int flag = this.baseMapper.delete(new QueryWrapper<CommentEntity>().eq("comment_id", commentId));
        if(flag == 0){
            return Resp.error(500,"删除失败，请确定是否存在该条评论");
        }else {
            return Resp.ok();
        }
    }

    @Override
    public Resp saveComment(CommentEntity comment) {
        comment.setMarkTime(new Date());
        comment.setCommentId(System.currentTimeMillis());
        int insert = this.baseMapper.insert(comment);
        if(insert == 0){
            return Resp.error(500,"插入失败");
        }else{
            return Resp.ok();
        }
    }

    @Override
    public PageUtils queryPageBatchList(HashMap<String,Object> hashMap) {
        String key = (String)hashMap.get("key");
        if(key.equals("")){
            IPage<CommentEntity> page = this.page(
                    new Query<CommentEntity>().getPage(hashMap),
                    new QueryWrapper<CommentEntity>().orderByDesc("mark_time")
            );
            return  new PageUtils(page);
        }else{
            IPage<CommentEntity> page = this.page(
                    new Query<CommentEntity>().getPage(hashMap),
                    new QueryWrapper<CommentEntity>().like("uid",hashMap.get("key")).or().like("video_id",hashMap.get("key")).or().like("comment_content",key)
            );
            return  new PageUtils(page);
        }


    }

    @Override
    public boolean batchDelete(Long[] commentIds) {
        try {
            commentDao.batchDelete(commentIds);
            return true;
        } catch (Exception e) {

            e.printStackTrace();
            return  false;
        }

    }


}